/**
*      author: Y_Yao
*      created: 2025-03-27 15:56:28
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 100 + 7;
int dp[N][N];
int n1,n2;
string s1,s2;
unordered_map<char,int> mp;

int b[6][6] = {
    {0,0,0,0,0,0},
	{0,5,-1,-2,-1,-3},
	{0,-1,5,-3,-2,-4},
	{0,-2,-3,5,-2,-2},
	{0,-1,-2,-2,5,-1},
	{0,-3,-4,-2,-1,0}
};

void solve()
{
    mp['A'] = 1;
    mp['C'] = 2;
    mp['G'] = 3;
    mp['T'] = 4;
    cin >> n1 >> s1;
    cin >> n2 >> s2;
    s1 = " " + s1;
    s2 = " " + s2;
    dp[0][0] = 0;
    for(int i = 1;i <= n2;i++)
    {
        dp[0][i] = dp[0][i-1] + b[mp[s2[i]]][5];
    }
    for(int i = 1;i <= n1;i++)
    {
        dp[i][0] = dp[i-1][0] + b[5][mp[s1[i]]];
    }
    for(int i = 1;i <= n1;i++)
    {
        for(int j = 1;j <= n2;j++)
        {
            dp[i][j] = max(dp[i-1][j-1] + b[mp[s1[i]]][mp[s2[j]]],max(dp[i-1][j] + b[mp[s1[i]]][5],dp[i][j-1] + b[5][mp[s2[j]]]));
        }
    }
    cout << dp[n1][n2] << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}